/*
 * @authors Ren Xiaofei
 * @date    2016-7-25
 * @version foamka_coffee
 */
(function($) {

	
	initFn();
	

	function initFn() {
		window.onscroll = function() {
			var top = document.documentElement.scrollTop || document.body.scrollTop;
			if(top == 0) {
				$("header").css("background", "rgba(0,0,0,0.5)");
				$("header .head_box .logo").attr("src", "img/logo1.png");
				$("header .head_box nav a").css("color", "#fff");
				$("header .head_box .none_nav a").css("color", "#fff");
			} else {
				$("header").css("background", "rgba(255,255,255,0.6)");
				$("header .head_box .logo").attr("src", "img/logo.png");
				$("header .head_box nav a").css("color", "#000");
				$("header .head_box .none_nav a").css("color", "#000");
			}

			//小火箭
			function crestFn() {
				if(top > 666) {
					$(".crest img").show(0);
				} else {
					$(".crest img").hide(0);
				}
				$(".crest img").on("click", function(event) {
					if(top > 0) {
						$('body').stop().animate({
							scrollTop: 0
						}, 1500);
					}
				});
			}
			crestFn();
		}

		var swiper = new Swiper('.swiper-container', {
			pagination: '.swiper-pagination',
			nextButton: '.swiper-button-next',
			prevButton: '.swiper-button-prev',
			paginationClickable: true,
			spaceBetween: 0,
			centeredSlides: true,
			autoplay: 2500,
			autoplayDisableOnInteraction: false
		});
		
		var lock = true;
		$("#scan").on("click", function() {	
			if(lock == false) {
				$("#scan_img").hide(0);
				lock = true;
			} else {
				$("#scan_img").show(0);
				lock = false;
			}
		});
		$("#scan").hover(function() {
			$("#scan_img").show(0);
		}, function() {
			if (lock) {
				$("#scan_img").hide(0);
			}
		});
		



		var p_boxTop = $(".content .content_down .activities_box .our_activities .pos_box .p_box").position().top;

		function activitiesFn() {
			$(".content .content_down .activities_box .our_activities .pos_box").hover(function() {
				$(this).find("span").stop().animate({
					height: 100+"%"
				}, 888);
				$(this).find(".p_box").stop().animate({
					bottom: 70 + "%"
				}, 888);
			}, function() {
				$(this).find("span").stop().animate({
					height: 1.5+"rem"
				}, 888);
				$(this).find(".p_box").stop().animate({
					bottom: 0
				}, 888);
			});
		}
		activitiesFn();

		//dudu
		$("header .head_box nav .dcode_nav").on("mouseover", function() {
			$("header .head_box nav .dcode_nav").show(0);
		});
		$("header .head_box nav .dcode_nav").on("mouseout", function() {
			$("header .head_box nav .dcode_nav").hide(0);
		});

		$("header .head_box nav a").eq(1).hover(function() {
			$("header .head_box nav .dcode_nav").css("display", "block");
		}, function() {
			$("header .head_box nav .dcode_nav").css("display", "none");
		});
		$("header .head_box .none_nav a").eq(1).hover(function() {
			$("header .head_box .none_nav .dcode_none_nav").css("display", "block");
		}, function() {
			$("header .head_box .none_nav .dcode_none_nav").css("display", "none");
		});
		$("header .head_box nav ul li").eq(5).on("mouseover", function() {
			$("header .head_box nav .none-join").css("display", "block");
		});
		$("header .head_box nav .none-join").on("mouseout", function() {
			$("header .head_box nav .none-join").hide(0);
		});

		$("header .head_box nav .none-join a").each(function() {
			$(this).hover(function() {
				$(this).css("color", "#FFF");
			}, function() {
				$(this).css("color", "#000");
			});
		});

		var loack = true;
		$("header .head_box .title-nav").on("click", function() {
			if(loack == true) {
				$("header .head_box .none_nav").css("display", "block");
				$("header .head_box .none-line").css("display", "block");
				$("header .head_box").css("height", "11rem");
				loack = false;
				console.log(1);
			} else {
				$("header .head_box .none_nav").css("display", "none");
				$("header .head_box .none-line").css("display", "none");
				$("header .head_box").css("height", "1.8rem");
				loack = true;
			}
		});

		function aaa() {
			if($("body").width() > 830) {
				$("header .head_box .none_nav").css("display", "none");
				$("header .head_box .none-line").css("display", "none");
				$("header .head_box").css("height", "1.8rem");
			}
		}
		setInterval(function() {
			aaa();
		}, 500);

		$(function() {
			$("header .head_box nav a,header .head_box .none_nav a").click(function() {
				if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
					var $target = $(this.hash);
					$target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');
					if($target.length) {
						var targetOffset = $target.offset().top;
						$('html,body').animate({
								scrollTop: targetOffset
							},
							1000);
						return false;
					}
				}
			});
		})

		function mapFn() {
			window.onload = function() {
				var height = $(".content .content_down .join_box #map .world_map").height();
				$(".content .content_down .join_box #map").css("height", height + "px");
			}
		}
		var timer = setInterval(mapFn, 500);
		var index = 0;
		$(".content .content_down .join_box #map .map_beijing:nth-of-type(1) img").on("click", function() {
			index++;
			if(index % 2 == 0) {
				$(this).attr("src", "img/rarrow-1.png").css({
					"width": "0.3rem",
					"left": "2.8rem",
					"top": "-5.7rem"
				});
				$(this).parents(".map_beijing").siblings(".beijing").hide(0);
			} else {
				$(this).attr("src", "img/downarrow.png").css({
					"width": "0.5rem",
					"left": "2.7rem",
					"top": "-5.6rem"
				});
				$(this).parents(".map_beijing").siblings(".beijing").show(0);
			}
		});

		$(".content .content_down .join_box #map .map_beijing:nth-of-type(2) li").each(function(index, element) {
			$(this).on("click", function() {
				baiduFn();
				clearInterval(timer);
				$(".content .content_down .join_box #map").css("height", 480 + "px");
			});
		});
	}

	//引入百度地图函数
	function baiduFn() {
		//创建和初始化地图函数：
		function initMap() {
			createMap(); //创建地图
			setMapEvent(); //设置地图事件
			addMapControl(); //向地图添加控件
			addMapOverlay(); //向地图添加覆盖物
		}

		function createMap() {
			map = new BMap.Map("map");
			map.centerAndZoom(new BMap.Point(116.44976, 39.926561), 14);
		}

		function setMapEvent() {
			map.enableScrollWheelZoom();
			map.enableKeyboard();
			map.enableDragging();
			map.enableDoubleClickZoom()
		}

		function addClickHandler(target, window) {
			target.addEventListener("click", function() {
				target.openInfoWindow(window);
			});
		}

		function addMapOverlay() {
			var markers = [{
				content: "北京市东城区东华门疯咔店",
				title: "东华门分店",
				imageOffset: {
					width: -46,
					height: -21
				},
				position: {
					lat: 39.932066,
					lng: 116.42485
				}
			}, {
				content: "北京市国贸疯咔分店",
				title: "国贸分店",
				imageOffset: {
					width: -46,
					height: -21
				},
				position: {
					lat: 39.912644,
					lng: 116.462112
				}
			}, {
				content: "北京市朝阳区望京疯咔店",
				title: "望京分店",
				imageOffset: {
					width: -46,
					height: -21
				},
				position: {
					lat: 39.925392,
					lng: 116.48204
				}
			}];
			for(var index = 0; index < markers.length; index++) {
				var point = new BMap.Point(markers[index].position.lng, markers[index].position.lat);
				var marker = new BMap.Marker(point, {
					icon: new BMap.Icon("http://api.map.baidu.com/lbsapi/createmap/images/icon.png", new BMap.Size(20, 25), {
						imageOffset: new BMap.Size(markers[index].imageOffset.width, markers[index].imageOffset.height)
					})
				});
				var label = new BMap.Label(markers[index].title, {
					offset: new BMap.Size(25, 5)
				});
				var opts = {
					width: 200,
					title: markers[index].title,
					enableMessage: false
				};
				var infoWindow = new BMap.InfoWindow(markers[index].content, opts);
				marker.setLabel(label);
				addClickHandler(marker, infoWindow);
				map.addOverlay(marker);
			};
			var labels = [];
			for(var index = 0; index < labels.length; index++) {
				var opt = {
					position: new BMap.Point(labels[index].position.lng, labels[index].position.lat)
				};
				var label = new BMap.Label(labels[index].content, opt);
				map.addOverlay(label);
			};
			var plOpts = [];
			var plPath = [];
			for(var index = 0; index < plOpts.length; index++) {
				var polyline = new BMap.Polyline(plPath[index], plOpts[index]);
				map.addOverlay(polyline);
			}
		}
		//向地图添加控件
		function addMapControl() {
			var scaleControl = new BMap.ScaleControl({
				anchor: BMAP_ANCHOR_BOTTOM_LEFT
			});
			scaleControl.setUnit(BMAP_UNIT_IMPERIAL);
			map.addControl(scaleControl);
			var navControl = new BMap.NavigationControl({
				anchor: BMAP_ANCHOR_TOP_LEFT,
				type: BMAP_NAVIGATION_CONTROL_LARGE
			});
			map.addControl(navControl);
			var overviewControl = new BMap.OverviewMapControl({
				anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
				isOpen: true
			});
			map.addControl(overviewControl);
		}
		var map;
		initMap();
	}

})(jQuery);